package cn.lyh.gulimall.product.service;

import cn.lyh.gulimall.product.dao.PmsAttrAttrgroupRelationMapper;
import cn.lyh.gulimall.product.dao.PmsAttrGroupMapper;
import cn.lyh.gulimall.product.dao.PmsAttrMapper;
import cn.lyh.gulimall.product.entity.PmsAttr;
import cn.lyh.gulimall.product.entity.PmsAttrAttrgroupRelation;
import cn.lyh.gulimall.product.entity.PmsAttrGroup;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.PageUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lyh
 * @version 1.0
 * @since 2021/4/6
 */
@Service
public class PmsAttrGroupService extends ServiceImpl<PmsAttrGroupMapper, PmsAttrGroup> {

	@Autowired
	PmsAttrAttrgroupRelationMapper relationMapper;

	@Autowired
	PmsAttrService attrService;
	@Autowired
	PmsAttrMapper attrMapper;


	/**
	 * 分页查询 无条件查询
	 *
	 * @param params 分页数据
	 * @return PageUtils
	 */
	public PageUtils queryPage(Map<String, Object> params) {
		// 先获取前端传入的参数
		String key = (String) params.get("key");
		// 构造查询条件
		QueryWrapper<PmsAttrGroup> queryWrapper = new QueryWrapper<>();
		// 获取分页数据
		long curPage = params.get("page") != null ? Long.parseLong((String) params.get("page")) : 1;
		long limit = params.get("limit") != null ? Long.parseLong((String) params.get("limit")) : 10;
		IPage<PmsAttrGroup> page = this.page(new Page<>(curPage, limit), queryWrapper);
		return new PageUtils(page);
	}

	/**
	 * 分页查询，根据Id来查询
	 *
	 * @param params    数据
	 * @param catelogId id数据
	 * @return PageUtils
	 */
	public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
		// 先获取前端传入的参数
		String key = (String) params.get("key");
		// 构造查询条件
		QueryWrapper<PmsAttrGroup> queryWrapper = new QueryWrapper<>();
		if (!StringUtils.isNullOrEmpty(key)) {
			queryWrapper.and((obj) -> {
				obj.eq(PmsAttrGroup.COL_ATTR_GROUP_ID, key).or().like(PmsAttrGroup.COL_ATTR_GROUP_NAME, key);
			});
		}

		// 获取分页数据
		long curPage = params.get("page") != null ? Long.parseLong((String) params.get("page")) : 1;
		long limit = params.get("limit") != null ? Long.parseLong((String) params.get("limit")) : 10;

		// 查询判断，如果Id为0则无条件查询，如果有Id则根据Id查询
		if (catelogId == 0) {
			IPage<PmsAttrGroup> page = this.page(new Page<>(curPage, limit), queryWrapper);
			return new PageUtils(page);
		} else {
			queryWrapper.eq(PmsAttrGroup.COL_CATELOG_ID, catelogId);
			IPage<PmsAttrGroup> page = this.page(new Page<>(curPage, limit), queryWrapper);
			return new PageUtils(page);
		}
	}

	public List<PmsAttr> getAttrRelation(Long attrgroupId) {
		List<PmsAttrAttrgroupRelation> relations = relationMapper.selectList(new QueryWrapper<PmsAttrAttrgroupRelation>().eq(PmsAttrAttrgroupRelation.COL_ATTR_GROUP_ID, attrgroupId));
		List<Long> attrIds = relations.stream().map(PmsAttrAttrgroupRelation::getAttrId).collect(Collectors.toList());
		if (attrIds.size() == 0) {
			return null;
		}
		List<PmsAttr> pmsAttrs = attrService.listByIds(attrIds);
		return pmsAttrs;
	}

	public PageUtils getAttrNoRelation(Long attrgroupId, Map<String, Object> params) {
		// 获取分页数据
		long curPage = params.get("page") != null ? Long.parseLong((String) params.get("page")) : 1;
		long limit = params.get("limit") != null ? Long.parseLong((String) params.get("limit")) : 10;

		// 1当前分组只能关联自己所属分类的所有属性
		PmsAttrGroup attrGroup = this.baseMapper.selectById(attrgroupId);
		Long catelogId = attrGroup.getCatelogId();
		// 2当前分组只能关联别的分组没有引用的属性
		//2.1 当前分类下的其他分组
		List<PmsAttrGroup> pmsAttrGroups =
				this.baseMapper.selectList(new QueryWrapper<PmsAttrGroup>()
						.eq(PmsAttrGroup.COL_CATELOG_ID, catelogId));
		List<Long> collect = pmsAttrGroups.stream().map(PmsAttrGroup::getAttrGroupId).collect(Collectors.toList());

		List<PmsAttrAttrgroupRelation> groupId = relationMapper.selectList(new QueryWrapper<PmsAttrAttrgroupRelation>().in(PmsAttrAttrgroupRelation.COL_ATTR_GROUP_ID, collect));
		List<Long> attrIds = groupId.stream().map(PmsAttrAttrgroupRelation::getAttrId).collect(Collectors.toList());

		//2.3从当前分类的所有属性中移除这些属性
		QueryWrapper<PmsAttr> attrQueryWrapper = new QueryWrapper<PmsAttr>().eq(PmsAttr.COL_CATELOG_ID, catelogId).eq(PmsAttr.COL_ATTR_TYPE,1);
		if (attrIds.size() != 0) {
			attrQueryWrapper.notIn(PmsAttr.COL_ATTR_ID, attrIds);
		}
		String key = (String) params.get("key");
		if (!StringUtils.isNullOrEmpty(key)) {
			attrQueryWrapper.and((w) -> {
				w.eq("attr_id", key).or().like("attr_name", key);
			});
		}
//		IPage<PmsAttr> page = this.page(new Page<>(curPage, limit), attrQueryWrapper);
		IPage<PmsAttr> page = new Page<>(curPage, limit);
		List<PmsAttr> pmsAttrs = attrMapper.selectList(attrQueryWrapper);
		page.setRecords(pmsAttrs);
		return new PageUtils(page);

	}
}
